//
//  PAAppContext.m
//  PilotSeries
//
//  Created by 黄立强 on 16/6/29.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "PAAppContext.h"
#import "RangeListData.h"
#import "ChartConfig.h"
#import "PAChartConfigManager.h"
@implementation PAAppContext
+ (PAAppContext *)sharedInstance {
    static PAAppContext *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
-(NSString *)w_host{
    switch (self.envType) {
        case PAEnvironmentTypeDev:
            return @"webchartingviewserver.fastexcelservices.idea-dev2.pilotcloud.paic.com.cn";
            break;
        case PAEnvironmentTypeQA:
            return @"webchartingviewserver.fastexcelservices.idea-uat.pilotcloud.paic.com.cn";
            break;
        case PAEnvironmentTypeProduct:
            return @"pilot-prod.paic.com.cn";
            break;
        default:
            return @"192.168.5.22";
            break;
    }
    
}

-(NSUInteger)w_port{
    switch (self.envType) {
        case PAEnvironmentTypeDev:
            return 11000;
            break;
        case PAEnvironmentTypeQA:
            return 11000;
            break;
        case PAEnvironmentTypeProduct:
            return 11000;
            break;
        default:
            return 21499;
            break;
    }
}

-(NSString *)chartingview_login_url{
    
    NSString *url;
    if (self.envType ==PAEnvironmentTypeDev ) {
        url = @"https://authnservice.bootstrapservices.idea-dev2.pilotcloud.paic.com.cn:10081/IDEA/assertion";
//        url = @"http://schedulercenter.coreservices.idea-dev.pilotcloud.paic.com.cn:10088/scheduler/login";
    }else if(self.envType ==PAEnvironmentTypeProduct){
        url = @"https://pilot-prod.paic.com.cn:10081/IDEA/assertion";
    }else if (self.envType ==PAEnvironmentTypeQA){
        url = @"https://pilot-uat.paic.com.cn:10081/IDEA/assertion";
        
        
    }
    
    return url;
    
    
}

-(void)processData:(NSDictionary *)dic{
   
    
    if ([dic[@"command"] isEqualToString:@"rangelist"]) {

        NSMutableArray *mArr=[RangeListData mj_objectArrayWithKeyValuesArray:dic[@"excelRanges"]];
        self.rangelists = mArr;
        if ([self.rangelistDelegete respondsToSelector:@selector(webSocketManagerDidReiceveRangeList:)]) {
         
            [self.rangelistDelegete webSocketManagerDidReiceveRangeList:mArr];
            
        }

    }else if([dic[@"command"] isEqualToString:@"metadata"]){
        self.metaData = dic[@"parameters"];
        
    }else if([dic[@"command"] isEqualToString:@"chartConfigList"]){
        NSArray *chartConfigs;
        if ([dic[@"chartConfigs"] isKindOfClass:[NSNull class]]) {
            chartConfigs = [NSArray new];
        }else{
            chartConfigs = dic[@"chartConfigs"];
//            DDLogWarn(@"sendMsg:%@",chartConfigs);
            
            
        }
        NSMutableArray *configs = [NSMutableArray arrayWithCapacity:chartConfigs.count];
      
        if (chartConfigs.count>0) {
            
            [chartConfigs enumerateObjectsUsingBlock:^(NSDictionary  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSMutableDictionary *mdic = [obj mutableCopy];
                mdic[CONFIG_KEY_HOST] = self.w_host;
                mdic[CONFIG_KEY_PORT] = @(self.w_port);
                mdic[CONFIG_KEY_CONFIGSOURCE] = w_cofingSource;
                mdic[CONFIG_KEY_URLS] = nil;
                mdic[CONFIG_KEY_CHARTNAME] = mdic[CONFIG_KEY_TITLE];
                mdic[CONFIG_KEY_APPID] = self.appid;
                if([mdic[@"parameters"] isKindOfClass:[NSNull class]] || mdic[@"parameters"] == nil){
                    mdic[@"parameters"] = [NSDictionary new];
                }
//                NSMutableDictionary *param = [mdic[@"parameters"] mutableCopy];
//                param[@"mergestrategy"] = @"INTERSECT";
//                param[@"ohlc.chartType"] = @"OHLC";
//                param[@"ohlc.grouping.relaxed"] = @YES;
//                param[@"ohlc.period"] = @"1h";
//                param[@"ohlc.start"] = @"-5d";
//                mdic[@"parameters"] = param;
                if(![mdic[CONFIG_KEY_RANGECONFIG] isKindOfClass:[NSNull class]]){
//                    NSMutableDictionary *param1 = [mdic[CONFIG_KEY_RANGECONFIG] mutableCopy];
//                    param1[@"maxRecords"] = [NSNumber numberWithInt:10000];
//                    mdic[CONFIG_KEY_RANGECONFIG] = param1;
                    
//                    NSMutableDictionary *param1 = [mdic[CONFIG_KEY_RANGECONFIG] mutableCopy];
//                    if(![param1[@"endTime"] isKindOfClass:[NSNull class]] && ![param1[@"endTime"] isEqualToString:@""]){
//                        long t = [param1[@"endTime"] longLongValue] - 7776000L*1000L;
//                        t = MAX([param1[@"startTime"] longLongValue],t);
//                        param1[@"startTime"] = [NSNumber numberWithLongLong: t];
//                    }else{
//                        long t =  ([[NSDate new] timeIntervalSince1970] - 7776000L)*1000L;
//                        t = MAX([param1[@"startTime"] longLongValue],t);
//                        param1[@"startTime"] = [NSNumber numberWithLongLong: t];
//                    }
//                    mdic[CONFIG_KEY_RANGECONFIG] = param1;
                }
                
                ChartConfig *chartConfig = [ChartConfig new];
                chartConfig.chartName = mdic[CONFIG_KEY_CHARTNAME];
                chartConfig.config = mdic;
                [configs addObject:chartConfig];
            }];
            
            
            NSArray *chartNames = [configs valueForKey:CONFIG_KEY_CHARTNAME];
            //修改本地的chartConfig
            NSMutableArray *currentConfigs = [[PAChartConfigManager loadCurrentChartConfigs] mutableCopy];
            if (currentConfigs.count>0) {
                [currentConfigs enumerateObjectsUsingBlock:^(ChartConfig  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSUInteger index = [chartNames indexOfObject:obj.chartName];
                    if (index!=NSNotFound) {
                        ChartConfig *serverChartConfig = (ChartConfig *) configs[index];
                        //更新
                        [PAChartConfigManager updateChartConfig:serverChartConfig];
                    }
                }];
            }
            
         
            [configs sortUsingComparator:^NSComparisonResult(ChartConfig   *obj1, ChartConfig *obj2) {
                return [obj1.chartName.lowercaseString compare: obj2.chartName.lowercaseString];
            }];
        }
        self.chartConfigs = configs;
        if ([self.chartConfiglistDelegete respondsToSelector:@selector(webSocketManagerDidReiceveChartConfigList:)]) {
            
            [self.chartConfiglistDelegete webSocketManagerDidReiceveChartConfigList:configs];
            
        }
    }
    
}

-(NSArray *)chartNames{
    
    return [self.chartConfigs valueForKey:@"chartName"];
}

-(NSString *)appid{
    if (_appid ==nil) {
        _appid =[NSString stringWithFormat:@"%@@Mobile",self.username];
    }
    return _appid;
}
@end
